<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="文件排序" prop="fileSort">
        <el-input
          v-model="queryParams.fileSort"
          placeholder="请输入文件排序"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文件名称" prop="fileName">
        <el-input
          v-model="queryParams.fileName"
          placeholder="请输入文件名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联新闻" prop="linkId">
        <el-select
          clearable
          v-model="queryParams.linkId"
          placeholder="请选择类型"
        >
          <el-option
            v-for="item in linkOptions"
            :key="item.id"
            :label="item.newsTitle"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="文件类型" prop="fileType">
        <el-select
          clearable
          v-model="queryParams.fileType"
          placeholder="请选择类型"
        >
          <el-option
            v-for="item in fileTypeList"
            :key="item.val"
            :label="item.label"
            :value="item.val"
          >
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="状态" prop="status">
        <el-select
          clearable
          v-model="queryParams.status"
          placeholder="请选择状态"
        >
          <el-option
            v-for="item in filestatusList"
            :key="item.val"
            :label="item.label"
            :value="item.val"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索
        </el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
        >重置
        </el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['portal:fileData:add']"
        >新增
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAddImgs"
          v-hasPermi="['portal:fileData:add']"
        >批量新增
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['portal:fileData:edit']"
        >修改
        </el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['portal:fileData:remove']"
        >删除
        </el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="fileDataList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="文件名称" align="center" prop="fileName"/>
      <el-table-column label="预览" align="center" prop="fileUrl" show-overflow-tooltip>
        <template slot-scope="scope">
          <div class="file_Url">
            <el-image v-if="scope.row.fileType === 0" fit="contain" :src="scope.row.fileUrl"
                      :preview-src-list="[scope.row.fileUrl]"></el-image>
            <!--            <img v-if="scope.row.fileType === 0" :src="scope.row.fileUrl" alt="" srcset="">-->
            <a v-if="scope.row.fileType === 1" :href="scope.row.fileUrl">
              <span :title="scope.row.fileUrl">点击下载</span>
            </a>
            <video v-if="scope.row.fileType === 2" :src="scope.row.fileUrl"></video>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="关联新闻" align="center" prop="newsTitle"/>
      <el-table-column label="文件类型" align="center" prop="fileType">
        <template slot-scope="scope">
          <span>{{
              scope.row.fileType === 0
                ? "图片"
                : scope.row.fileType === 1
                  ? "文档"
                  : scope.row.fileType === 2
                    ? "视频"
                    : "音乐"
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="KEY" align="center" prop="fileKey"/>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <span>{{
              scope.row.fileType === 0
                ? "正常"
                : '停用'
            }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" width="300px">
        <template slot-scope="scope">
          <div style="width: 100%;overflow: hidden;text-overflow: ellipsis;white-space: nowrap">{{
              scope.row.remark
            }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['portal:fileData:edit']"
          >修改
          </el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['portal:fileData:remove']"
          >删除
          </el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />


    <el-dialog title="批量添加" :visible.sync="showUpload" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文件名称" prop="fileName">
          <el-input v-model="form.fileName" placeholder="请输入文件名称"/>
        </el-form-item>
        <el-form-item label="文件key" prop="fileKey">
          <el-input v-model="form.fileKey" placeholder="请输入文件标识"/>
        </el-form-item>
        <el-form-item
          label="图片列表"
          prop="fileUrls"
          class="upload-form-item"
        >
          <el-upload
            multiple
            :action="uploadImgUrl"
            ref="imgUpload"
            :file-list="upImgList"
            :on-success="handleUploadSuccess"
            :auto-upload="true"
            accept=".png,.jpg,.jpeg"
            list-type="picture">
            <el-button slot="trigger">选择图片</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <!--<el-button @click="cancel">取 消</el-button>-->
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listFileData, delFileData} from "@/api/portal/fileData";
import {addImages} from "@/api/system/fileList";
import {listNews} from "@/api/portal/news";
import ImageUpload from "@/components/ImageUpload";

export default {
  name: "FileData",
  components: {
    ImageUpload
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/system/file/upload", // 上传的图片服务器地址
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      showUpload: false,
      open: true,
      resFileUrlList: [],
      upImgList: [],
      form: {
        fileName: '',
        status: '0',
        fileType: 0
      },
      rules: {
        fileName: [{required: true, message: '请输入文件名称', trigger: 'blur'}]
      },
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文件数据表格数据
      fileDataList: [],
      // 弹出层标题
      title: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fileSort: null,
        fileName: null,
        fileUrl: null,
        linkId: null,
        fileType: null,
        status: null,
        groupKey: null
      },
      fileTypeList: [
        {label: '图片', val: 0},
        {label: '文档', val: 1},
        {label: '视频', val: 2},
        {label: '音乐', val: 3},
      ],
      filestatusList: [
        {label: '正常', val: '0'},
        {label: '停用', val: '1'},
      ],
      groupKey: '',

      linkOptions: []
    };
  },
  created() {
    if (this.$route.query.groupKey) {
      this.queryParams.groupKey = this.$route.query.groupKey;
      this.groupKey = this.$route.query.groupKey;
      this.getList();
    }
  },
  mounted() {
    this.getnewsList()
  },
  methods: {
    /** 查询新闻列表 */
    getnewsList() {
      listNews(this.queryParams).then((response) => {
        this.linkOptions = response.rows;
      });
    },
    /** 查询文件数据列表 */
    getList() {
      this.loading = true;
      listFileData(this.queryParams).then(response => {
        this.fileDataList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push({
        name: "Editfile",
        query: {groupKey: this.groupKey},
      });
    },
    //批量新增
    handleAddImgs() {
      this.form = {
        fileName: '',
        status: '0',
        fileType: 0
      }
      this.resFileUrlList=[]
      this.showUpload = true
    },
    handleUploadSuccess(res) {
      let url = "http://" + res.data.url
      this.resFileUrlList.push(url)
    },
    submitForm() {
      if (this.isEmpty(this.form.fileName)) {
        this.msgError("请输入文件名称");
        return
      }
      if (this.resFileUrlList.length < 1) {
        this.msgError("请选择图片");
        return
      }
      let fileDatas = []
      this.resFileUrlList.forEach(url => {
        let obj = {
          "fileName": this.form.fileName,
          "fileUrl": url,
          "fileType": 0,
          "groupKey": this.groupKey,
          "status": "0"
        }
        fileDatas.push(obj)
      })
      addImages(fileDatas).then(res => {
        if (res.code===200){
          this.msgSuccess(res.msg)
          this.getList()
          this.showUpload=false
          this.upImgList=[]
        }else {
          this.msgError(res.msg)
        }
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.$router.push({
        name: "Editfile",
        query: {id: row.id || this.ids, groupKey: this.groupKey},
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除文件数据编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delFileData(ids);
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/fileData/export', {
        ...this.queryParams
      }, `system_fileData.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
.file_Url {
  height: 100px;
  width: 100px;
  display: flex;
  line-height: 100px;
  margin: 0 auto;

  img, video, a {
    width: 100%;
    height: 100%;
  }

  img {
    height: auto;
  }
}
</style>
